package com.sykj.why.controller.admin.album;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.AlbumT;
import com.sykj.why.mongo.dto.entity.AlbumTDTO;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AlbumTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/6/4
 * @desc 后台专辑管理控制访问层
 */

@RestController
@RequestMapping("/admin/album")
@Slf4j
@Api(value = "/admin/album",description = "后台专辑管理控制访问层")
public class AlbumController {

    @Autowired
    private AlbumTService albumTService;

    /**
     * 添加专辑
     * @param albumT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加专辑")
    public ResponseEntity add(@RequestBody AlbumT albumT){
        try {
            albumTService.saveAlbum(albumT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加专辑出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除专辑
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove/{id}",notes = "删除专辑")
    public ResponseEntity remove(@PathVariable String id){
        try {
            albumTService.removeAlbum(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除专辑出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改专辑
     * @param albumT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改专辑")
    public ResponseEntity update(@RequestBody AlbumT albumT){
        try {
            albumTService.updateAlbum(albumT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改专辑出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 启用/禁用专辑
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    @ApiOperation(value = "/enable",notes = "启用/禁用专辑")
    public ResponseEntity enable(@RequestParam String id,@RequestParam boolean flag){
        try {
            albumTService.disableOrEnable(id, flag);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("启用禁用专辑出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询全部专辑
     *
     * @return 全部专辑
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value ="/findAll",notes = "查询全部专辑")
    public ResponseEntity findAll() {
        try {
            return Response.success("查询成功", albumTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部专辑出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 分页查询专辑
     * @param albumTDTO
     * @return
     */
    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    @ApiOperation(value = "/findByPage",notes = "分页查询专辑")
    public ResponseEntity findByPage(@RequestBody AlbumTDTO albumTDTO){
        try {
            return Response.success("查询成功", albumTService.findByPage(albumTDTO));
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("分页查询专辑出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
